﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Duckbill
{
    public class Bullet : GameObject
    {
        public Vector2 vel = Vector2.Zero;
        public Bullet(string sprite, bool playerBullet, Vector2 pos, Vector2 vel) : base(sprite, pos)
        {
            if (playerBullet)
                this.type = ObjectType.playerAttack;
            else
                this.type = ObjectType.enemyAttack;
            this.vel = vel;
            angle = Facing();
        }

        float Facing()
        {
            if (vel.Length() == 0)
                return 0f;
            float h = (float)Math.Acos(vel.X/vel.Length());
            if (vel.Y < 0)
                h += (float)Math.PI;
            return h;
        }

        public override void Update(float time, Dictionary<ObjectType, List<GameObject>> others)
        {
            //check collision against tiles
            foreach (Tile tile in others[ObjectType.tile])
                if (/*tile.tileType == TileType.solid &&*/
                    !(Rectangle.Intersect(getRect(), tile.getRect()).IsEmpty))
                    toRemove = true;

            //check collision against players or enemies
            List<GameObject> toCheck;
            if (type == ObjectType.playerAttack)
                toCheck = others[ObjectType.enemy];
            else
                toCheck = others[ObjectType.player];
            foreach (Enemy enemy in toCheck)
            {
                if (!enemy.toRemove && !(Rectangle.Intersect(getRect(), enemy.getRect()).IsEmpty))
                {
                    enemy.health -= 5;
                    if (enemy.health <= 0)
                        enemy.toRemove = true;
                    toRemove = true;
                }
            }
        }
        public override void Move(float time)
        {
            pos += vel*time;
        }
    }
}
